<script lang="ts" setup>
import type { AuthApi } from '#/api/types';

import { useVbenDrawer } from '@vben/common-ui';

import { message } from 'ant-design-vue';

import { useVbenForm } from '#/adapter/form';
import { registerApi } from '#/api/core/modules/auth';
import { $t } from '#/locales';

const emits = defineEmits(['success']);

// 创建用户表单
const [Form, formApi] = useVbenForm({
  schema: [
    {
      component: 'Input',
      fieldName: 'username',
      label: $t('system.user.username'),
      rules: 'required',
      componentProps: {
        placeholder: '请输入用户名(4-20个字符)',
      },
    },
    {
      component: 'InputPassword',
      fieldName: 'password',
      label: $t('system.user.password'),
      rules: 'required',
      componentProps: {
        placeholder: '请输入密码(6-20个字符)',
      },
    },
    {
      component: 'InputPassword',
      fieldName: 'confirmPassword',
      label: $t('system.user.confirmPassword'),
      rules: 'required',
      componentProps: {
        placeholder: '请再次输入密码',
      },
    },
    {
      component: 'Input',
      fieldName: 'nickname',
      label: $t('system.user.nickname'),
      componentProps: {
        placeholder: '请输入昵称',
      },
    },
    {
      component: 'Input',
      fieldName: 'email',
      label: $t('system.user.email'),
      componentProps: {
        placeholder: '请输入邮箱',
      },
    },
    {
      component: 'Input',
      fieldName: 'phone',
      label: $t('system.user.phone'),
      componentProps: {
        placeholder: '请输入手机号',
      },
    },
    {
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        options: [
          { label: $t('common.enabled'), value: 1 },
          { label: $t('common.disabled'), value: 0 },
        ],
        optionType: 'button',
      },
      defaultValue: 1,
      fieldName: 'status',
      label: $t('system.user.status'),
    },
  ],
  showDefaultActions: false,
});

// 抽屉组件
const [Drawer, drawerApi] = useVbenDrawer({
  title: $t('common.create', [$t('system.user.name')]),

  // 表单确认处理
  async onConfirm() {
    try {
      // 表单验证
      const { valid } = await formApi.validate();
      if (!valid) return false;

      // 获取表单值
      const values = await formApi.getValues();

      // 验证密码
      if (values.password !== values.confirmPassword) {
        message.error($t('common.passwordMismatch'));
        return false;
      }

      // 锁定抽屉
      drawerApi.setState({ loading: true });

      // 创建用户
      const registerParams: AuthApi.RegisterParams = {
        username: values.username,
        password: values.password,
        nickname: values.nickname || '',
        email: values.email,
        phone: values.phone,
      };

      const result = await registerApi(registerParams);
      if (result.success) {
        message.success($t('common.operationSuccess'));
        emits('success');

        // 重置表单
        formApi.resetForm();

        // 主动关闭抽屉
        drawerApi.close();
        return true;
      } else {
        message.warning(result.message || $t('common.operationFailed'));
        return false;
      }
    } catch {
      return false;
    } finally {
      drawerApi.setState({ loading: false });
    }
  },

  // 打开关闭处理
  onOpenChange(isOpen) {
    if (isOpen) {
      // 重置表单
      formApi.resetForm();
    }
  },
});

// 对外暴露抽屉API
defineExpose({ drawerApi });
</script>

<template>
  <Drawer>
    <Form />
  </Drawer>
</template>
